package per.turtle.common.datasource.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import per.turtle.common.core.utils.SecurityUtils;

import java.time.LocalDateTime;

/**
 * 元对象处理
 *
 * @author yuj
 */
@Component
public class TurtleMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        String userId = SecurityUtils.getUserId();
        this.fillStrategy(metaObject, "accessTime", LocalDateTime.now());
        this.fillStrategy(metaObject, "createTime", LocalDateTime.now());
        this.fillStrategy(metaObject, "createBy", userId);
        this.fillStrategy(metaObject, "updateTime", LocalDateTime.now());
        this.fillStrategy(metaObject, "updateBy", userId);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String userId = SecurityUtils.getUserId();
        this.fillStrategy(metaObject, "updateTime", LocalDateTime.now());
        this.fillStrategy(metaObject, "updateBy", userId);
    }

}
